<template>
  <div id="NextConstructionContract">
    <div class="iframeWrap">
      <iframe :src="iframeUrl" frameborder="0" width="100%" height="100%"></iframe>
    </div>
    <div class="footer">
      <van-button size="small" type="info" @click="save">签字</van-button>
    </div>
  </div>
</template>
<script>
import { signGJ, signNH, LoadGridData, updateDataView, QuerySignCallback } from "../../utils/api";
import { v4 as uuidv4 } from 'uuid';
export default {
  name: "NextConstructionContract",
  data() {
    return {
      iframeUrl: "",
      rowData: "",
    };
  },
  methods: {
    // 保存
    save: async function () {
      let htRowData = JSON.parse(this.$route.query.htRowData);
      let userInfo = window.localStorage.getItem("userName");
      //生成uuid
      let uuid = uuidv4()
      this.$store.commit('getUUID1', uuid)
      window.localStorage.setItem('UUID1', uuid)
      let params = {
        callbackUrl: "https://nfzjszlb.jst.zj.gov.cn", //可以写死
        callbackParam: "fileGuid=bed4afda-d7c8-45a2-ac58-068ee5e3f832", //可以写死
        fileUrl: `http://10.145.12.79:28800/PrintServer/api/PreviewWordRpt?groupGuid=%7B3C9A4EE5-CEF6-4AE5-AEE9-EF15D8665975%7D&toRowGuid=${this.rowData.ROW_GUID}&dataGuid=e2b255e7-11e5-4279-983d-5275b5b5ca98`,
        isPerson: "1", //可以写死
        pdfFileFlag: this.$store.state.UUID1,
        personDTO: {
          handSignWay: "0",
          isFreeSign: "0",
          username: htRowData.CBR,
          mobile: htRowData.CBRLXDH,
          idnum: htRowData.CBRJMSFZJM,
          idType: "1",
          signPositionDTO: {
            keyword: "乙方：",
            llx: "0",
            lly: "0",
          },
        },
      };
      const res1 = await signGJ(params);
      if (res1.data.data) {
        //id值需要存一份  方便农户签订合同
        this.ZWFWHTID = JSON.parse(res1.data.data).data
        // var formData2 = [htRowData];
        // let parms = {
        //   dataGuid: "e83485a7-0e53-40b9-b881-cf184792be89",
        //   strParams: JSON.stringify(formData2),
        // };
        // const res2 = await updateDataView(parms);
        if (ZWJSBridge && JSON.parse(res1.data.data).data) {
          ZWJSBridge.openLink({
            url:
              "https://zjseal.zjzwfw.gov.cn/api/tyyzpt/sign/doViewDoc?id=" +
              JSON.parse(res1.data.data).data,
          })
            .then((res) => {
              //
            })
            .catch((err) => {
            });
        } else {
          this.$toast.fail(JSON.parse(res1.data.data).errorMsg);
        }
      }
    },
  },
  mounted: async function () {
    //先判断是否签订合同
    let userInfo = window.localStorage.getItem("userName");
    this.rowData = JSON.parse(this.$route.query.rowData);
    window.localStorage.setItem('rowData', JSON.stringify(this.rowData))
    this.iframeUrl = `${window.g.BaseURL}/PrintServer/api/PreviewWordRpt?groupGuid=%7B3C9A4EE5-CEF6-4AE5-AEE9-EF15D8665975%7D&toRowGuid=${this.rowData.ROW_GUID}&dataGuid=e2b255e7-11e5-4279-983d-5275b5b5ca98`
    if (ZWJSBridge) {
      ZWJSBridge.setTitle({
        title: "惠州市低层农村住房建设施工合同",
      })
        .then((res) => {
        })
        .catch((err) => {
        });
    }
    //先调用签完合同的回调函数 
    if (this.$store.state.UUID1 || window.localStorage.getItem('UUID1')) {
      let params = {
        "accessCode": "143d5ac31ee240cb8dc294f0e886bcf6",
        "appKey": "6738b790-69e8-11ec-a673-6805cab640a0",
        "params":
        {
          "pdfFileFlag": this.$store.state.UUID1 ? this.$store.state.UUID1 : window.localStorage.getItem('UUID1')
        }
      }
      const res1 = await QuerySignCallback(params)
      //判断是农户还是工匠签完了合同
      if (res1.data.data != '' || res1.data.data.length != 0) {
        var formData2 = [{
          PKID: this.rowData.PKID,
          row_guid: this.rowData.ROW_GUID,
          iszrsguid: this.ZWFWHTID,
          // HTGUID: res1.data[0].fileguid
        }]
        let parms = {
          dataGuid: '01103ebf-a884-4282-b996-78dd64196946',
          strParams: JSON.stringify(formData2),
           token: window.localStorage.getItem('token')
        }
        const res1 = await updateDataView(parms)
        if (res1.data.Success == true) {
          this.$toast.success('更新成功!');
          // me.$notify({ type: 'success', message: '更新成功!' });
          this.$router.push({
            path: "/ConstructionContract",
            query: { data: JSON.stringify(this.rowData) },
          });
        }
      }
    } else {

    }
  },
};
</script>
<style lang="scss" scoped>
#NextConstructionContract {
  width: 100%;
  height: 100vh;
  .iframeWrap {
    width: 100%;
    height: calc(100% - 40px);
  }
  .footer {
    width: 100%;
    text-align: center;
    margin-top: 4px;
  }
}
</style>
